$(document).ready(function(){
	vm.load();
		$("#page_title").html("食物列表");
});
var food_submit = {};
/**
 * 获取地址栏中的名称
 */
food_submit.get_name = function(){
	return decodeURI(common.getUrlParam('name'));
};
var vm = avalon.define({
	$id:"main_controller",
	//----------------
	//toast的信息
	toast_msg:'',
	//是否要展示toast
	is_toast_show:false,
	//定时器
	$timeout:0,
	//消息隐藏
	clear_toast:function(){
		//设置定时器
		if(vm.$timeout){
			clearTimeout(vm.$timeout);
		}
		vm.$timeout = setTimeout(function(){
			vm.is_toast_show = false;
			vm.toast_msg = '';
		},3000);		
	},	
	//-----------------		
	//名称
	name:food_submit.get_name(),
	//总值
	all:0,
	//列表
	list:[],
	//是否正在加载
	is_loading:false,
	//加载数据
	load:function(){
		vm.is_loading = true;
		$.ajax({
			type:"post",
			url:get_memdailyfood_list_url,
			success:function(data){
				var all = 0;
				for(var i=0;i<data.length;i++){
					all += new Number(data[i].foodenergy);
				}
				vm.all = all;
				vm.list = data;
				//执行按需加载
				setTimeout(function(){
					$(".target_div img").unveil();
					vm.is_loading = false;
				},100);					
			},
			error:function(){
				vm.is_loading = false;
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();						
			},
			data:"dictlibraryid="+common.getUrlParam("dictlibraryid")+"&date="+common.getUrlParam('date'),
			dataType:"json",
		});		
	},	
	//删除
	delete_submit:function(index){
		var obj = vm.list[index];
		var $this = $(this);
		$this.addClass("fa-spin fa-fw");
		$this.find("i").removeClass("fa-times").addClass("fa-spinner");
		var fd = new FormData();
		fd.append("id",obj.id);
		$.ajax({
			type:"post",
			url:delect_memdailyfood_url,
			dataType:"json",
			data:fd,			
			processData: false,
			contentType: false,				
			success:function(data){
				$this.removeClass("fa-spin fa-fw");
				$this.find("i").removeClass("fa-spinner").addClass("fa-times");
				vm.toast_msg = data.msg;
				if(data.status == 1){
					vm.list.removeAt(index);
				}
				vm.is_toast_show = true;
				vm.clear_toast();						
			},	
			error:function(){
				$this.removeClass("fa-spin fa-fw");
				$this.find("i").removeClass("fa-spinner").addClass("fa-times");
				vm.toast_msg = "网络错误";
				vm.is_toast_show = true;
				vm.clear_toast();					
			},			
		});			
	},	
	go_to_food:function(){
		location.href =food_url+'?date='+common.getUrlParam('date')+'&dictlibraryid='+common.getUrlParam('dictlibraryid');
	},
});
avalon.scan();
avalon.ready(function(){
	$('body').css('opacity',1);
});
